package com.onemile.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onemile.app.base.BaseUINoBottom;
import com.onemile.data.DXml;
import com.onemile.entity.MessageCode;
import com.onemile.entity.PubConst;
import com.onemile.entity.Session;
import com.onemile.entity.model.EUser;
import com.onemile.net.NetClient;
import com.onemile.utils.UtiDialogsFactory;

public class UILogin extends BaseUINoBottom
{
   private int parentActiveID;

   private ImageView imgLogin;
   private ImageView imgRegister;
   private EditText edtName;
   private EditText edtPwd;


   private void InitControls()
   {
	  edtName = (EditText) findViewById(R.id.edtUserName_login);
	  edtPwd = (EditText) findViewById(R.id.edtPwd_login);
	  imgRegister=(ImageView)findViewById(R.id.imgRegister_login);
	  imgRegister.setOnClickListener(new View.OnClickListener()
	  {
	     @Override
	     public void onClick(View v)
	     {
	  	    Intent intent=new Intent();
	  	    intent.setClass(UILogin.this, UIRegister.class);
	  	    startActivity(intent);
	     }
	  });
	  imgLogin = (ImageView) findViewById(R.id.imgLogin_login);
	  imgLogin.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			if (VerifyInfo())
			{
			   executeAsyncTask(PubConst.TaskName.LOGIN);
			}
		 }
	  });
   }

   private boolean VerifyInfo()
   {
	  if (edtName.getText().toString().trim() == null || edtName.getText().toString().trim().equals(""))// 用户名为空
	  {
		 UtiDialogsFactory.getInstance().ShowToastInfoShort(UILogin.this, getString(R.string.login_user_not_empty));
		 return false;
	  }
	  else if (edtPwd.getText().toString().trim() == null || edtPwd.getText().toString().trim().equals(""))// 密码为空
	  {
		 UtiDialogsFactory.getInstance().ShowToastInfoShort(UILogin.this, getString(R.string.login_pwd_not_empty));
		 return false;
	  }
	  return true;
   }


   private void GOOrderList()
   {
	  Intent intent = new Intent();
	  intent.setClass(UILogin.this, UIOrderList.class);
	  startActivity(intent);
   }

   private void GOFavoriteList()
   {
	  Intent intent = new Intent();
	  intent.setClass(UILogin.this, UIFavoriteList.class);
	  startActivity(intent);
   }

   private void GOCouponList()
   {
	  Intent intent = new Intent();
	  intent.setClass(UILogin.this, UICouponList.class);
	  startActivity(intent);
   }

   private void GoSettlement()
   {
	  Intent intent = new Intent();
	  intent.setClass(UILogin.this, UISettlement.class);
	  startActivity(intent);
   }

   @Override
   protected void onCreateOverride(Bundle savedInstanceState)
   {
	  InitControls();

   }

   @Override
   public int SetContentViewLayoutResId()
   {
	  return R.layout.login;
   }

   @Override
   public void SetTopToolbar(TextView topTxt, ImageView leftImBtn, ImageView rightImgBtn, RelativeLayout rightRay, TextView cartNum)
   {
	  topTxt.setText(getString(R.string.title_login));
	  parentActiveID = getIntent().getIntExtra(PubConst.Intent_Key.LOGIN_FROM_ACTIVITY, 0);
   }

   @Override
   public void onAsyncTaskStart(String taskName, Object[] params)
   {
	  UtiDialogsFactory.getInstance().ShowLoadingDialogNoTitle(UILogin.this, getString(R.string.dialog_loading));
   }

   @Override
   public Object onAsyncTaskInBackground(String taskName, Object[] params) throws Exception
   {
	  if (taskName.equals(PubConst.TaskName.LOGIN))
	  {
		 try
		 {
			return NetClient.Login(edtName.getText().toString().trim(), edtPwd.getText().toString().trim());
		 }
		 catch (Exception e)
		 {
			if (Session.getInstance().IsDEBUG())
			{
			   Log.e("ERROR", "UILogin_onAsyncTaskInBackground(taskName, params) ", e);
			}
		 }
	  }
	  return null;
   }

   @Override
   public void onAsyncTaskEnd(String taskId, String taskName, Object[] params, Object result)
   {
	  try
	  {
		 if (taskName.equals(PubConst.TaskName.LOGIN))
		 {
			UtiDialogsFactory.getInstance().DismissLoadingDialog();
			if (result != null)
			{
			   DXml data = (DXml) result;
			   if (data.getHead().GetFieldValue("RspCode").equals(MessageCode.Success.Code))
			   {
				  EUser user = new EUser();
				  user.setUserId(data.getBody().GetFieldValue("UserId"));
				  user.setUserName(data.getBody().GetFieldValue("UserName"));
				  user.setPassword(data.getBody().GetFieldValue("Password"));
				  user.setMobile(data.getBody().GetFieldValue("Mobile"));
				  user.setEmail(data.getBody().GetFieldValue("Email"));
				  user.setUserType(Integer.valueOf(data.getBody().GetFieldValue("UserType")));
				  Session.getInstance().setUserInfo(user);
				  if (parentActiveID == PubConst.ActivityCode.LOGIN)
				  {
					 setResult(Activity.RESULT_OK);
				  }
				  else if (parentActiveID == PubConst.ActivityCode.CART)
				  {
					 setResult(Activity.RESULT_OK);
					 GoSettlement();
				  }
				  else if (parentActiveID == PubConst.ActivityCode.FAVORITE_LIST)
				  {
					 setResult(Activity.RESULT_OK);
					 GOFavoriteList();
				  }
				  else if (parentActiveID == PubConst.ActivityCode.ORDER_LIST)
				  {
					 setResult(Activity.RESULT_OK);
					 GOOrderList();

				  }
				  else if (parentActiveID == PubConst.ActivityCode.COUPON_LIST)
				  {
					 setResult(Activity.RESULT_OK);
					 GOCouponList();
				  }
				  finish();
			   }
			   else
			   {
				  UtiDialogsFactory.getInstance().ShowToastInfoShort(UILogin.this, data.getHead().GetFieldValue("RspMsg"));
			   }
			}
		 }
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UILogin_onAsyncTaskEnd(taskId, taskName, params, result) ", e);
		 }
	  }
   }

}
